package ua.org.nuos.sdms.middle.entity;

import javax.persistence.*;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 09.10.11
 * Time: 20:34
 * User's group
 */
@Entity
@Table(name = "group_")
@NamedQueries(value = {
        @NamedQuery(name = Group.QUERY_FIND_BY_NAME, query = "SELECT DISTINCT g FROM Group g WHERE LOWER(g.name) LIKE :groupName ORDER BY g.name"),
        @NamedQuery(name = Group.QUERY_FIND_BY_ORGANISATION, query = "SELECT DISTINCT g FROM Group g WHERE LOWER(g.organisation) LIKE :organisation ORDER BY g.name")
})
public class Group implements Serializable {

    public static enum Type {
        OPENED, CLOSED
    }

    private static final String CUSTOM_SEQUENCE = "custom_sequence";

    public static final String QUERY_FIND_BY_NAME = "findGroupByName";
    public static final String QUERY_FIND_BY_ORGANISATION = "findGroupsByOrganisation";

    @Id
    @Column(name = "id")
    @SequenceGenerator(name = CUSTOM_SEQUENCE, initialValue = 20)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = CUSTOM_SEQUENCE)
    private long id;

    @Size(max = 255)
    @Column(name = "name")
    private String name;

    @Lob
    @Column(name = "about")
    private String about;

    @NotNull
    @Past
    @Column(name = "create_date", nullable = false)
    @Temporal(value = TemporalType.DATE)
    private Date createDate;

    @Size(max = 255)
    @Column(name = "organisation")
    private String organisation;

    @NotNull
    @Column(name = "type", nullable = false)
    @Enumerated(value = EnumType.STRING)
    private Type type;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getOrganisation() {
        return organisation;
    }

    public void setOrganisation(String organisation) {
        this.organisation = organisation;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Group group = (Group) o;

        if (id != group.id) return false;
        if (about != null ? !about.equals(group.about) : group.about != null) return false;
        if (createDate != null ? !createDate.equals(group.createDate) : group.createDate != null) return false;
        if (name != null ? !name.equals(group.name) : group.name != null) return false;
        if (organisation != null ? !organisation.equals(group.organisation) : group.organisation != null) return false;
        if (type != null ? !type.equals(group.type) : group.type != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (about != null ? about.hashCode() : 0);
        result = 31 * result + (createDate != null ? createDate.hashCode() : 0);
        result = 31 * result + (organisation != null ? organisation.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Group{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", about='" + about + '\'' +
                ", createDate=" + createDate +
                ", organisation='" + organisation + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
